package com.cosyhut.blocks.jode;

import com.jme.math.*;
import com.cosyhut.blocks.factory.*;

/**
 * 
 * @author TimWorks
 * Feb 04, 2007
 *
 * 
 */
public class ODEGeom extends ODESpatial {
	private MGeom _model;
	
    public ODEGeom(MGeom model) {
        super();
        this._model = model;
    	createGeom();
        jode.dGeomTransformSetGeom(this._transform, this._geom);        
    }
    
    public void attach(ODEBlock block, Vector3f position, Matrix3f rotation) {
        if (null != this._block) {
            this.detach();
        }
        if (null != block) {
            this._block = block;
            jode.dGeomSetBody(this._transform, block.getBody());
            jode.dSpaceAdd(block.getSpace(), this._transform);

            this.setPosition(position);
            this.setRotation(rotation);
        }
    }

    protected void detach() {
        jode.dGeomSetBody(this._transform, null);
        jode.dSpaceRemove(this._block.getSpace(), this._transform);

        this._block = null;
    }
    //--------------------------------------------------------------------------
    private void createGeom() {
    	if (jodeConstants.dBoxClass == this._model.getType()) {
    		MGeomBox m = (MGeomBox)this._model;
            this._geom = jode.dCreateBox(null, m.getX(), m.getY(), m.getZ());		
    	} else if (jodeConstants.dSphereClass == this._model.getType()) {
        		MGeomSphere m = (MGeomSphere)this._model;
                this._geom = jode.dCreateSphere(null, m.getRadius());
    	} else if (jodeConstants.dCylinderClass == this._model.getType()) {
    		MGeomCylinder m = (MGeomCylinder)this._model;
            this._geom = jode.dCreateCylinder(null, m.getRadius(), m.getLength());
    	} else {
    		assert(false);
    	}
    }
    //--------------------------------------------------------------------------
    public MGeom getModel() {
    	return this._model;
    }

}
